var Camera = {
    DestinationType: {
        DATA_URL: 0,         // Return base64 encoded string
        FILE_URI: 1,         // Return file uri (content://media/external/images/media/2 for Android)
        NATIVE_URI: 2        // Return native uri (eg. asset-library://... for iOS)
    },
    EncodingType: {
        JPEG: 0,             // Return JPEG encoded image
        PNG: 1               // Return PNG encoded image
    },
    MediaType: {
        PICTURE: 0,          // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
        VIDEO: 1,            // allow selection of video only, ONLY RETURNS URL
        ALLMEDIA: 2         // allow selection from all media types
    },
    PictureSourceType: {
        PHOTOLIBRARY: 0,    // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
        CAMERA: 1,          // Take picture from camera
        SAVEDPHOTOALBUM: 2  // Choose image from picture library (same as PHOTOLIBRARY for Android)
    },
    PopoverArrowDirection: {
        ARROW_UP: 1,        // matches iOS UIPopoverArrowDirection constants to specify arrow location on popover
        ARROW_DOWN: 2,
        ARROW_LEFT: 4,
        ARROW_RIGHT: 8,
        ARROW_ANY: 15
    },
    Direction: {
        BACK: 0,
        FRONT: 1
    }
};

navigator.camera = cameraExport = {};

for (var key in Camera) {
    cameraExport[key] = Camera[key];
}

cameraExport.getPicture = function (successCallback, errorCallback, options) {
    //argscheck.checkArgs('fFO', 'Camera.getPicture', arguments);
    options = options || {};
    var getValue = function (value, defaultValue) {
        return value === undefined ? defaultValue : value;
    };
    var quality = getValue(options.quality, 50);
    var destinationType = getValue(options.destinationType, Camera.DestinationType.FILE_URI);
    var sourceType = getValue(options.sourceType, Camera.PictureSourceType.CAMERA);
    var targetWidth = getValue(options.targetWidth, -1);
    var targetHeight = getValue(options.targetHeight, -1);
    var encodingType = getValue(options.encodingType, Camera.EncodingType.JPEG);
    var mediaType = getValue(options.mediaType, Camera.MediaType.PICTURE);
    var allowEdit = !!options.allowEdit;
    var correctOrientation = !!options.correctOrientation;
    var saveToPhotoAlbum = !!options.saveToPhotoAlbum;
    var popoverOptions = getValue(options.popoverOptions, null);
    var cameraDirection = getValue(options.cameraDirection, Camera.Direction.BACK);
    var fileName = options.fileName
    var args = [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType,
        mediaType, allowEdit, correctOrientation, saveToPhotoAlbum, popoverOptions, cameraDirection, fileName];

    cordova.exec(successCallback, errorCallback, "Camera", "takePicture", args);
    // XXX: commented out
    //return new CameraPopoverHandle();
};

cameraExport.cleanup = function (successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, "Camera", "cleanup", []);
};


/*function openMyCamera(){
 // 使用设备上的摄像头拍照，并获得Base64编码字符串格式的可编辑图像
 navigator.camera.getPicture(onPhotoURISuccess, function(err){
 }, {fileName:userID, quality: 90,targetWidth:320,targetHeight:680,allowEdit: true });
 }

 function onPhotoURISuccess(imageURI) {
 var largeImage = document.getElementById('testCamera');
 largeImage.style.display = 'block';
 largeImage.src = imageURI;
 largeImage.style.width = 100;
 largeImage.style.width = 200;
 }*/

function openCamera(callback) {
    // 使用设备上的摄像头拍照，并获得Base64编码字符串格式的可编辑图像
    navigator.camera.getPicture(function (url) {
        callback(url)
    }, function (err) {
    }, {fileName: vars.userInfo.userID, saveToPhotoAlbum: false, quality: 100/*, targetHeight:360, targetWidth: 640*/});
}

function getPhoto(callback) {

    // 从设定的来源处获取图像文件URI
    navigator.camera.getPicture(function (url) {

        cordova.exec(function (data) {
            var returnobj = {
                result: true,
                imgFileURLForHTMLDisplay: data
            };
            callback(returnobj);
        }, function (err) {
            alert(err)
        }, "FileHelper", "copyFile", [url]);


    }, function () {
    }, {fileName:  vars.userInfo.userID, targetWidth: 320, targetHeight: 680, quality: 90, destinationType: 1, sourceType: 0});
}
